Completed
Push — master ( 3627f6...11ce7e )
by Yannick
30:00
created

map.common.js ➔ clickIVAO   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 1
1
/**
2
 * This javascript is part of FlightAirmap.
3
 *
4
 * Copyright (c) Ycarus (Yannick Chabanois) <[email protected]>
5
 * Licensed under AGPL license.
6
 * For more information see: https://www.flightairmap.com/
7
*/
8
function getCookie(cname) {
9
    var name = cname + "=";
10
    var ca = document.cookie.split(';');
11
    for(var i=0; i<ca.length; i++) {
12
	var c = ca[i];
13
	while (c.charAt(0)==' ') c = c.substring(1);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
14
	if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
15
    }
16
    return "";
17
}
18
19
function delCookie(cname) {
20
    document.cookie = cname + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
21
}
22
23
function createCookie(name, value, days) {
24
    var date, expires;
25
    if (days) {
26
	date = new Date();
27
	date.setTime(date.getTime()+(days*24*60*60*1000));
28
	expires = "; expires="+date.toGMTString();
29
    } else {
30
	expires = "";
31
    }
32
    document.cookie = name+"="+value+expires+"; path=/";
33
}
34
35
function dynamicSort(property) {
36
    var sortOrder = 1;
37
    if(property[0] === "-") {
38
        sortOrder = -1;
39
        property = property.substr(1);
40
    }
41
    return function (a,b) {
42
        var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
43
        return result * sortOrder;
44
    }
45
}
46
47
function dynamicSortMultiple() {
48
    var props = arguments;
49
    return function (obj1, obj2) {
50
	var i = 0, result = 0, numberOfProperties = props.length;
51
	while(result === 0 && i < numberOfProperties) {
52
	    result = dynamicSort(props[i])(obj1, obj2);
53
	    i++;
54
	}
55
	return result;
56
    }
57
}
58
59
function mapType(selectObj) {
60
    var idx = selectObj.selectedIndex;
61
    var atype = selectObj.options[idx].value;
62
    var type = atype.split('-');
63
    if (type[0] == 'Mapbox') {
64
	createCookie('MapType',type[0],9999);
65
	createCookie('MapTypeId',type[1],9999);
66
	if (getCookie('Map2D3DSync')) {
67
	    createCookie('MapType3D',type[0],9999);
68
	    createCookie('MapType3DId',type[1],9999);
69
	}
70
    } else {
71
	createCookie('MapType',atype,9999);
72
	if (getCookie('Map2D3DSync')) {
73
	    createCookie('MapType3D',atype,9999);
74
	}
75
    }
76
    window.location.reload();
77
}
78
function mapType3D(selectObj) {
79
    var idx = selectObj.selectedIndex;
80
    var atype = selectObj.options[idx].value;
81
    var type = atype.split('-');
82
    if (type[0] == 'Mapbox') {
83
	createCookie('MapType3D',type[0],9999);
84
	createCookie('MapType3DId',type[1],9999);
85
	if (getCookie('Map2D3DSync')) {
86
	    createCookie('MapType',type[0],9999);
87
	    createCookie('MapTypeId',type[1],9999);
88
	}
89
    } else {
90
	createCookie('MapType3D',atype,9999);
91
	if (getCookie('Map2D3DSync')) {
92
	    createCookie('MapType',atype,9999);
93
	}
94
    }
95
    window.location.reload();
96
}
97
function clickSyncMap2D3D(cb) {
98
    createCookie('Map2D3DSync',cb.checked,9999);
99
    if (cb.checked) {
100
	createCookie('MapType3D',getCookie('MapType'),9999);
101
	createCookie('MapType3DId',getCookie('MapTypeId'),9999);
102
    }
103
}
104
105
function terrainType(selectObj) {
106
    var idx = selectObj.selectedIndex;
107
    var atype = selectObj.options[idx].value;
108
    var type = atype.split('-');
109
    document.cookie =  'MapTerrain='+type+'; expires=Thu, 2 Aug 2100 20:47:11 UTC; path=/'
110
    createCookie('MapTerrain',type,9999);
111
    if (type == 'stk') {
112
	stkterrain();
113
    } else if (type == 'articdem') {
114
	articterrain();
115
    } else if (type == 'ellipsoid') {
116
	ellipsoidterrain();
117
    } else if (type == 'vrterrain') {
118
	vrtheworldterrain();
119
    }
120
    //window.location.reload();
121
}
122
123
function sattypes(selectObj) {
124
    var sattypes = [], sattype;
125
    for (var i=0, len=selectObj.options.length; i< len;i++) {
126
	sattype = selectObj.options[i];
127
	if (sattype.selected) {
128
	    sattypes.push(sattype.value);
129
	}
130
    }
131
    createCookie('sattypes',sattypes.join(),2);
132
    updateSat();
133
}
134
function airlines(selectObj) {
135
    var airs = [], air;
136
    for (var i=0, len=selectObj.options.length; i< len;i++) {
137
	air = selectObj.options[i];
138
	if (air.selected) {
139
	    airs.push(air.value);
140
	}
141
    }
142
    createCookie('filter_Airlines',airs.join(),2);
143
}
144
function airlinestype(selectObj) {
145
    var idx = selectObj.selectedIndex;
146
    var airtype = selectObj.options[idx].value;
147
    createCookie('filter_airlinestype',airtype,2);
148
}
149
function racefilter(selectObj) {
150
    var idx = selectObj.selectedIndex;
151
    var race = selectObj.options[idx].value;
152
    if (race == 'all') {
153
	delCookie('filter_race');
154
    } else {
155
	createCookie('filter_race',race,2);
156
    }
157
    if (getCookie['MapFormat'] == '3d') {
158
	updateMarineData();
159
    } else {
160
	getLiveMarineData(0);
161
    }
162
}
163
function alliance(selectObj) {
164
    var idx = selectObj.selectedIndex;
165
    var alliance = selectObj.options[idx].value;
166
    createCookie('filter_alliance',alliance,2);
167
}
168
function identfilter() {
169
    var ident = $("#identfilter").value;
170
    createCookie('filter_ident',ident,2);
171
}
172
function mmsifilter() {
173
    var ident = $("#mmsifilter").value;
174
    createCookie('filter_mmsi',ident,2);
175
}
176
function removefilters() {
177
    // Get an array of all cookie names (the regex matches what we don't want)
178
    var cookieNames = document.cookie.split(/=[^;]*(?:;\s*|$)/);
179
    // Remove any that match the pattern
180
    for (var i = 0; i < cookieNames.length; i++) {
181
	if (/^filter_/.test(cookieNames[i])) {
182
	    delCookie(cookieNames[i]);
183
	}
184
    }
185
    window.location.reload();
186
}
187
function sources(selectObj) {
188
    var sources = [], source;
189
    for (var i=0, len=selectObj.options.length; i< len;i++) {
190
	source = selectObj.options[i];
191
	if (source.selected) {
192
	    sources.push(source.value);
193
	}
194
    }
195
    createCookie('filter_Sources',sources.join(),2);
196
}
197
198
199
function show2D() {
200
    createCookie('MapFormat','2d',10);
201
    if (document.getElementById("pointtype").className == 'tracker') {
202
	createCookie('MapTrackTracker',document.getElementById("pointident").className,1);
203
    } else if (document.getElementById("pointtype").className == 'marine') {
204
	createCookie('MapTrackMarine',document.getElementById("pointident").className,1);
205
    } else {
206
	createCookie('MapTrack',document.getElementById("pointident").className,1);
207
    }
208
    window.location.reload();
209
}
210
function show3D() {
211
    createCookie('MapFormat','3d',10);
212
    if (document.getElementById("pointtype").className == 'tracker') {
213
	createCookie('MapTrackTracker',document.getElementById("pointident").className,1);
214
    } else if (document.getElementById("pointtype").className == 'marine') {
215
	createCookie('MapTrackMarine',document.getElementById("pointident").className,1);
216
    } else {
217
	createCookie('MapTrack',document.getElementById("pointident").className,1);
218
    }
219
    window.location.reload();
220
}
221
function clickPolar(cb) {
222
    createCookie('polar',cb.checked,9999);
223
    window.location.reload();
224
}
225
function clickDisplayAirports(cb) {
226
    createCookie('displayairports',cb.checked,9999);
227
    window.location.reload();
228
}
229
function clickDisplayISS(cb) {
230
    createCookie('displayiss',cb.checked,9999);
231
    updateSat();
232
}
233
function clickDisplayMinimap(cb) {
234
    createCookie('displayminimap',cb.checked,9999);
235
    window.location.reload();
236
}
237
function clickShadows(cb) {
238
    createCookie('map3dnoshadows',cb.checked,9999);
239
    window.location.reload();
240
}
241
function clickSingleModel(cb) {
242
    createCookie('singlemodel',cb.checked,9999);
243
}
244
function clickUpdateRealtime(cb) {
245
    createCookie('updaterealtime',cb.checked,9999);
246
}
247
function clickVATSIM(cb) {
248
    createCookie('filter_ShowVATSIM',cb.checked,2);
249
}
250
function clickIVAO(cb) {
251
     createCookie('filter_ShowIVAO',cb.checked,2);
252
}
253
function clickphpVMS(cb) {
254
    createCookie('filter_ShowVMS',cb.checked,2);
255
}
256
function clickSBS1(cb) {
257
    createCookie('filter_ShowSBS1',cb.checked,2);
258
}
259
function clickBlocked(cb) {
260
    createCookie('filter_blocked',cb.checked,2);
261
}
262
function clickAPRS(cb) {
263
    createCookie('filter_ShowAPRS',cb.checked,2);
264
}
265
function clickDisplayGroundStation(cb) {
266
    createCookie('show_GroundStation',cb.checked,2);
267
    window.location.reload();
268
}
269
function clickDisplayWeatherStation(cb) {
270
    createCookie('show_WeatherStation',cb.checked,2);
271
    window.location.reload();
272
}
273
/*
274
function clickDisplayWeather(cb) {
275
    createCookie('show_Weather',cb.checked,2);
276
//    window.location.reload();
277
}
278
*/
279
function clickDisplayLightning(cb) {
280
    createCookie('show_Lightning',cb.checked,2);
281
    window.location.reload();
282
}
283
function clickDisplayFires(cb) {
284
    createCookie('show_Fires',cb.checked,2);
285
    window.location.reload();
286
}
287
function clickDisplay2DBuildings(cb) {
288
    createCookie('Map2DBuildings',cb.checked,2);
289
    window.location.reload();
290
}
291
292
function unitdistance(selectObj) {
293
    var idx = selectObj.selectedIndex;
294
    var unit = selectObj.options[idx].value;
295
    createCookie('unitdistance',unit,9999);
296
}
297
function unitspeed(selectObj) {
298
    var idx = selectObj.selectedIndex;
299
    var unit = selectObj.options[idx].value;
300
    createCookie('unitspeed',unit,9999);
301
}
302
function unitcoordinate(selectObj) {
303
    var idx = selectObj.selectedIndex;
304
    var unit = selectObj.options[idx].value;
305
    createCookie('unitcoordinate',unit,9999);
306
}
307
function unitaltitude(selectObj) {
308
    var idx = selectObj.selectedIndex;
309
    var unit = selectObj.options[idx].value;
310
    createCookie('unitaltitude',unit,9999);
311
}
312
313
function addarchive(begindate,enddate) {
314
    console.log('Add archive');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
315
    createCookie('archive',true,2);
316
    createCookie('archive_begin',begindate,2);
317
    createCookie('archive_end',enddate,2);
318
    createCookie('archive_speed',document.getElementById("archivespeed").value,2);
319
    window.location.reload();
320
}
321
function noarchive() {
322
    console.log('Exit archive!');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
323
    delCookie('archive');
324
    delCookie('archive_begin');
325
    delCookie('archive_end');
326
    delCookie('archive_speed');
327
    window.location.reload();
328
}
329
function msgbox(text,buttontext) {
330
	buttontext = buttontext || "OK";
331
	$("<div>" + text + "</div>").dialog({
332
	    dialogClass: "no-close",
333
	    buttons: [{
334
		text: buttontext,
335
		click: function() {
336
		    $( this ).dialog( "close" );
337
		    $(this).remove();
338
		}
339
	    }]
340
	});
341
}
342
function generateRandomPoint (latitude,longitude,height,diff,radius) {
343
344
	//console.log('height: '+height+' - diff: '+diff);
345
	radius = Math.random()*radius;
346
	latitude = latitude*(Math.PI/180.0);
347
	longitude = longitude*(Math.PI/180.0);
348
	
349
	const sinLat = 	Math.sin(latitude)
350
	const cosLat = 	Math.cos(latitude)
351
352
	/* go fixed distance in random direction*/
353
	const bearing = Math.random() * Math.PI*2
354
	const theta = radius/6371000
355
	const sinBearing = Math.sin(bearing)
356
	const cosBearing = Math.cos(bearing)
357
	const sinTheta = Math.sin(theta)
358
	const cosTheta = Math.cos(theta)
359
    
360
	latitude = Math.asin(sinLat*cosTheta+cosLat*sinTheta*cosBearing);
361
	longitude = longitude + Math.atan2( sinBearing*sinTheta*cosLat, cosTheta-sinLat*Math.sin(latitude ));
362
	/* normalize -PI -> +PI radians */
363
	longitude = ((longitude+(Math.PI*3))%(Math.PI*2))-Math.PI
364
	var h = height+(Math.random()*diff)
365
	//console.log('h: '+h);
366
	return {
367
	    latitude: latitude/(Math.PI/180.0),
368
	    longitude: longitude/(Math.PI/180.0),
369
	    height: h
370
	};
371
}
372
function getColor(colorStart,colorEnd,colorCount,step) {
373
	var alpha = (1.0/colorCount)*step;
374
	return {
375
	    r: colorStart[0]*alpha+(1-alpha)*colorEnd[0],
376
	    v: colorStart[1]*alpha+(1-alpha)*colorEnd[1],
377
	    b: colorStart[2]*alpha+(1-alpha)*colorEnd[2]
378
	};
379
}
380
function convertDMS(coord,latlong) {
381
	if (latlong == 'latitude') {
382
		var nsew = (coord >= 0) ? 'N' : 'S';
383
	} else if (latlong == 'longitude') {
384
		var nsew = (coord >= 0) ? 'E' : 'W';
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable nsew already seems to be declared on line 382. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
385
	}
386
	var coord = Math.abs(coord);
387
	var deg = Math.floor(coord);
388
	var min = Math.floor((coord - deg) * 60);
389
	var sec = Math.round((coord - deg - min / 60) * 3600);
390
	var result = deg+"° "+min+"' "+sec+'" '+nsew;
0 ignored issues
show
Bug introduced by
The variable nsew does not seem to be initialized in case latlong == "longitude" on line 383 is false. Are you sure this can never be the case?
Loading history...
391
	return result;
392
}
393
function convertDM(coord,latlong) {
394
	if (latlong == 'latitude') {
395
		var nsew = (coord >= 0) ? 'N' : 'S';
396
	} else if (latlong == 'longitude') {
397
		var nsew = (coord >= 0) ? 'E' : 'W';
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable nsew already seems to be declared on line 395. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
398
	}
399
	var coord = Math.abs(coord);
400
	var deg = Math.floor(coord);
401
	var min = Math.round((coord - deg) * 60 *1000)/1000;
402
	var result = deg+"° "+min+"' "+nsew;
0 ignored issues
show
Bug introduced by
The variable nsew does not seem to be initialized in case latlong == "longitude" on line 396 is false. Are you sure this can never be the case?
Loading history...
403
	return result;
404
}
405